<?php

namespace App\Admin\Controllers;

use App\Models\Commodity;
use App\Models\CommodityAttr;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Table;
use Illuminate\Http\Request;


class CommodityController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '商品';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Commodity);

        $grid->model()->where('is_deleted', 0);

        $grid->column('id', __('Id'));
        // $grid->column('commodity_name', __('商品名称'))->expand(function ($model) {
        //     $comments = $model->commodityattr()->get()->map(function ($comment) {
        //         return $comment->only(['id', 'attr_name', 'attr_value', 'stock_num']);
        //     });
        //     return new Table(['ID', '属性值', '属性名', '库存'], $comments->toArray());
        // });
        $grid->column('commodity_name', __('商品名称'))->modal('商品属性', function ($model) {

            $comments = $model->commodityattr()->get()->map(function ($comment) {
                return $comment->only(['id', 'attr_value', 'pick_code','stock_num']);
            });

            return new Table(['ID', '规格', '搜索码','库存'], $comments->toArray());
        });

        $grid->brand()->name(__('所属品牌'));
        $grid->type()->name(__('所属类型'));

        $grid->column('commodity_desc', __('商品描述'));
        $grid->column('pictures', '商品封面')->display(function($value){
            return $value[0];
        })->gallery(['width' => 50, 'height' => 50]);
        // $grid->column('pick_num', __('拣货号'));
        $grid->column('power_price', __('消耗算力'));

        $states = [
            'on' => ['value' => 1, 'text' => '上架', 'color' => 'primary'],
            'off' => ['value' => 0, 'text' => '下架', 'color' => 'default'],
        ];
        $grid->column('shelves', __('是否上架'))->switch($states);

        $grid->column('sort', __('排序'));
        // $grid->column('is_deleted', __('是否删除'))->editable('select', [0 => '正常', 2 => '冻结']);
        $grid->column('created_at', __('创建时间'));

        

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Commodity::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('brand_id', __('Brand id'));
        $show->field('commodity_code', __('Commodity code'));
        $show->field('commodity_name', __('Commodity name'));
        $show->field('commodity_desc', __('Commodity desc'));
        $show->field('pick_num', __('Pick num'));
        $show->field('power_price', __('Power price'));
        $show->field('shelves', __('Shelves'));
        $show->field('sort', __('Sort'));
        $show->field('is_deleted', __('Is deleted'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Commodity);

        $form->text('commodity_name', __('商品名称'))->required();
        $form->select('brand_id', __('品牌'))->options('/admin/brands/get_brand_list');
        $form->select('commodity_type', __('类型'))->options('/admin/commoditytype/get_commodity_type_list');
        // $form->text('commodity_code', __('商品编号'))->required();

        $form->text('commodity_desc', __('商品描述'))->required();
        // $form->text('pick_num', __('拣货号'))->required();
        $form->decimal('power_price', __('算力消耗'))->required();
        $states = [
            'on' => ['value' => 1, 'text' => '上架', 'color' => 'primary'],
            'off' => ['value' => 0, 'text' => '下架', 'color' => 'default'],
        ];

        $form->hasMany('commodityattr', '商品属性', function (Form\NestedForm $form) {
            $form->text('attr_value', '规格');
            // $form->number('stock_num', '库存')->default(0);
            $form->text('pick_code', '搜索码')->default(0);
        });

        $form->multipleImage('pictures', '图片')->move('images/commodity/' . date("Ym/d", time()))->uniqueName()->sortable()->removable()->help('请框选图片或按住ctrl键选择多张图片,第一张图为默认封面图');

        $form->switch('shelves', __('是否上架'))->states($states);
        $form->number('sort', __('排序'))->default(0);

        $form->saving(function (Form $form) {
            // $code = $form->pictures;
            // dd($code);
        });
        return $form;
    }

    public function comList()
    {
        return Commodity::where('shelves',1)->get(['id','commodity_name as text']);
    }
    public function attrList(Request $request)
    {
        $com_id = $request->get('q');
        
        return CommodityAttr::where('commodity_id',$com_id)->get(['id','attr_value as text']);
    }


}
